package org.enate.bloatterrorist;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;


public final class PrefsFragment extends Fragment {
    private static final String TAG = "PrefsFragment";
    private static View v = null;
    private static TextView tv = null;
    private static Button b = null;
    private static ImageView iv1 = null, iv2 = null;
    private static SeekBar sb1 = null, sb2 = null;
    
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_prefs, container, false);
        return v;
    }
    
    @Override public void onActivityCreated(Bundle savedInstanceState) {
    	super.onActivityCreated(savedInstanceState);
    	v = getView();
    	prettyPrincess();
    	ActionItem.build_PF_AI();
    }
    
    private static void prettyPrincess(){
    	tv = (TextView)v.findViewById(R.id.banner);
    	
    	// ------------ Gridview Item Click Pref
    	iv1 = (ImageView)v.findViewById(R.id.iv_pref_gvclick_glow_appfilternormal);
    	iv2 = (ImageView)v.findViewById(R.id.iv_pref_gvclick_glow_appfilterpress);
    	b = (Button)v.findViewById(R.id.btn_pref_gvclick);
    	updatePrefState(b);
    	b.setOnTouchListener(API.otl_HTWAMA(iv1, iv2, false));
    	b.setTag("durp 1");
    	b.setOnClickListener(new OnClickListener(){@Override public void onClick(View v) {OnPrefItemClick(v);}});
    	
    	// ------------ Filter IME Toggle Pref
    	iv1 = (ImageView)v.findViewById(R.id.iv_pref_filterIME_normal);
    	iv2 = (ImageView)v.findViewById(R.id.iv_pref_filterIME_press);
    	b = (Button)v.findViewById(R.id.btn_pref_filterIME);
    	updatePrefState(b);
    	b.setOnTouchListener(API.otl_HTWAMA(iv1, iv2, false));
    	b.setTag("durp 2");
    	b.setOnClickListener(new OnClickListener(){@Override public void onClick(View v) {OnPrefItemClick(v);}});
    	
    	// ----------- Background Color Pref:
    	iv1 = (ImageView)v.findViewById(R.id.iv_pref_backgroundColor_normal);
    	iv2 = (ImageView)v.findViewById(R.id.iv_pref_backgroundColor_press);
    	b = (Button)v.findViewById(R.id.btn_pref_backgroundColor);
    	updatePrefState(b);
    	b.setOnTouchListener(API.otl_HTWAMA(iv1, iv2, false));
    	b.setOnClickListener(new OnClickListener(){@Override public void onClick(View v) {OnPrefItemClick(v);}});
    	
    	
    	// ----------- Columns Number Pref:
    	iv1 = (ImageView)v.findViewById(R.id.iv_pref_columnsnumber_normal);
    	iv2 = (ImageView)v.findViewById(R.id.iv_pref_columnsnumber_press);
    	sb1 = (SeekBar)v.findViewById(R.id.sb_pref_columnsnumber);
    	tv = (TextView)v.findViewById(R.id.tv_pref_columnsnumber);
    	iv1.setVisibility(View.GONE);
    	iv2.setVisibility(View.GONE);
    	sb1.setVisibility(View.GONE);
    	tv.setVisibility(View.GONE);
    	/*
    	sb1.setMax(12);
    	sb1.setProgress(Main.prefs.getInt(API.str_pref_gv_numCols, 4));
    	updatePrefState(sb1);
    	sb1.setOnSeekBarChangeListener(API.osbcl());
    	*/
    }

    private static void updatePrefState(View view){
    	switch(view.getId()){
    	case R.id.btn_pref_gvclick:
        	((TextView)view).setText(Html.fromHtml(
            	"<b>Icon press will:&nbsp;&nbsp;</b>" + 
            	"<small>"+(Main.prefs.getInt(API.str_pref_gvi_pressaction, 0) == 0 ? "show app menu":"launch (long-press for menu)" )+"</small>"
        	));
        	break;
    	case R.id.btn_pref_filterIME:
        	((TextView)view).setText(Html.fromHtml(
               	"<b>Swiping to filter will:&nbsp;&nbsp;</b>" + 
               	"<small>"+(Main.prefs.getInt(API.str_pref_filterpref_ime, 0) == 0 ? "show keypad":"not show keypad" )+"</small>"
           	));
        	break;
    	case R.id.btn_pref_backgroundColor:
    		((TextView)view).setText(Html.fromHtml(
    			"<b>Background color:&nbsp;&nbsp;</b>"+
    			"<small>(ARGB)&nbsp;#"+Integer.toHexString(Main.prefs.getInt(API.str_pref_backgroundColor, Color.BLACK)).toUpperCase()+"</small>"
    		));
    		break;
    	case R.id.sb_pref_columnsnumber:
    		((TextView)v.findViewById(R.id.tv_pref_columnsnumber)).setText(Html.fromHtml(
        			"<b>Number of columns in grid:&nbsp;&nbsp;</b>"+
        			"<small>"+((SeekBar)view).getProgress()+"</small>"
        	));
    		break;
    	}
    }
    
	private static void OnPrefItemClick(final View view){
		QuickAction.qa = new QuickAction(Main.c, QuickAction.VERTICAL);
		switch(view.getId()){
		case R.id.btn_pref_gvclick:
        	QuickAction.qa.addActionItem(ActionItem.action_pref_gvi_pressaction_showqa);
        	QuickAction.qa.addActionItem(ActionItem.action_pref_gvi_pressaction_launch);
        	QuickAction.qa.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {			
        		public void onItemClick(QuickAction source, int pos, int actionId) {
       				Main.prefs_editor.putInt(API.str_pref_gvi_pressaction, pos).commit();
       				updatePrefState(view);
        		}
        	});
        	QuickAction.qa.show(view);
        	break;
		case R.id.btn_pref_filterIME:
        	QuickAction.qa.addActionItem(ActionItem.action_pref_filterpref_ime_show);
        	QuickAction.qa.addActionItem(ActionItem.action_pref_filterpref_ime_hide);
        	QuickAction.qa.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {			
        		public void onItemClick(QuickAction source, int pos, int actionId) {
       				Main.prefs_editor.putInt(API.str_pref_filterpref_ime, pos).commit();
       				updatePrefState(view);
        		}
        	});
        	QuickAction.qa.show(view);
			break;
		case R.id.btn_pref_backgroundColor:
			API.showColorPickerDialog(R.id.btn_pref_backgroundColor, R.id.btn_pref_backgroundColor);
			break;
		}
	}
    
    protected static void onColorChangedCallback(int color) {
    	Main.prefs_editor.putInt(API.str_pref_backgroundColor, color).commit();
    	PagerAdapter.mPager.setBackgroundColor(color);
    	updatePrefState(v.findViewById(R.id.btn_pref_backgroundColor));
    }
    
    protected static void onSeekBarStateChangedCallback(SeekBar sb) {
    	Main.prefs_editor.putInt(API.str_pref_gv_numCols, sb.getProgress()).commit();
    	AppListFragment.gv.setNumColumns(sb.getProgress());
    	updatePrefState(sb);
    }
    
    
    
    
    
}